﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Reborn_Server.App1AppHandler;
using App1AppHandler;
using Reborn_WorldServer;
using System.IO;
using System.Xml;
using Reborn_WorldServer.App1AppHandler;

namespace Reborn_Server.App1AppHandler
{
    public class Potion_Recall : Buff, IConsumable
    {
        public Potion_Recall()
        {
            minutesLeft = -1;
        }

        private int recallMapId;
        private int recallInstance;
        private int recallMapX;
        private int recallMapY;

        public bool MeetsConsumeRequirements(Character character, Loot loot)
        {
            return true;
        }

        public override void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            StringReader sr = new StringReader(xmlData);
            XmlTextReader xmlReader = new XmlTextReader(sr);
            while (xmlReader.Read())
            {
                if (xmlReader.Name == "xmldata" && xmlReader.IsStartElement())
                {
                    recallMapId = int.Parse(xmlReader.GetAttribute("mapid"));
                    recallMapX = int.Parse(xmlReader.GetAttribute("mapx"));
                    recallMapY = int.Parse(xmlReader.GetAttribute("mapy"));
                    recallInstance = int.Parse(xmlReader.GetAttribute("instance"));
                }
            }
            if (recallInstance == targetCreatureBase.Instance)
                base.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);
        }

        public void Consume(Character myCharacter, Loot lootitem)
        {
            TargetCreatureBase = myCharacter;

            LockItem lockItem = TargetCreatureBase.GameHandler.GetLockItem(TargetCreatureBase.MapID, TargetCreatureBase.Instance);
            lock (lockItem.LockObj)
            {
                foreach (MapItem mapitem in TargetCreatureBase.currentMapItem.mapItemsWithin8)
                {
                    if (Math.Abs(mapitem.MapX - myCharacter.MapX) <= 8 && Math.Abs(mapitem.MapY - myCharacter.MapY) <= 8)
                    {
                        foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                        {
                            if (creaturebase is Character)
                            {
                                Character character = (Character)creaturebase;
                                character.PlaySound("DrinkBottle", myCharacter.MapX, myCharacter.MapY);
                                character.AddCreatureBase(myCharacter);
                            }
                        }
                    }
                }

                recallMapId = TargetCreatureBase.MapID;
                recallInstance = TargetCreatureBase.Instance;
                recallMapX = TargetCreatureBase.MapX;
                recallMapY = TargetCreatureBase.MapY;

                SpellIcon potion = null;
                foreach (KeyValuePair<int, SpellIcon> si in Managers.GameHandler.Spells)
                {
                    if (si.Value.Name == "Recall Potion")
                    {
                        potion = si.Value.clone();
                        break;
                    }
                }

                if (potion == null)
                {
                    potion = new SpellIcon();

                    this.SpellID = TargetCreatureBase.GameHandler.ItemIDGen.GetSpellID();
                    potion.ClassName = lootitem.ClassName;
                    potion.Description = lootitem.Description;
                    potion.ImageURL = lootitem.ImageURL;
                    potion.Name = lootitem.Name;
                    potion.Offensive = false;
                    potion.CastType = "Self";
                }

                this.SpellID = Managers.GameHandler.ItemIDGen.GetSpellID();
                this.spellIcon = potion;

                //Remove existing recall potions
                foreach (Buff buff in TargetCreatureBase.buffManager.Buffs)
                {
                    if (buff.spellIcon.Name == base.spellIcon.Name)
                    {
                        buff.RemoveSpell(false, false);
                    }
                }
                TargetCreatureBase.buffManager.AddBuff(this);
                TargetCreatureBase.buffManager.UpdateBuffs();
            }
        }



        private void preServerTransferMethod()
        {
            //Try to stop the round timer, this will only return true, if it was running, and was stopped
            //This means they are perma busy until they hit the other server,
            //If we return false, they are not busy due to lag or something, set them busy before transfer.
            Character character = (Character)TargetCreatureBase;

            if (character.tryStopRoundTimer() == false)
                character.SetBusy(true);
        }

        public override string GetXmlData()
        {
            return "<xmldata mapid=\"" + recallMapId.ToString() + "\" mapx=\"" + recallMapX.ToString() + "\" mapy=\"" + recallMapY.ToString() + "\" instance=\"" + recallInstance.ToString() + "\"/>";
        }

        public override void RemoveSpell(bool updateBuffs, bool sendMessage)
        {
            base.RemoveSpell(updateBuffs, sendMessage);

            if (sendMessage == false || updateBuffs == false)
                return;

            if (TargetCreatureBase.HP < 1)
            {
                TargetCreatureBase.SendMessage("Cannot recall while dead!", "server");
                TargetCreatureBase.PlaySound("Ding", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                return;
            }

            string recallResult = Managers.GameHandler.Teleport((Character)TargetCreatureBase, recallMapId, recallMapX, recallMapY, preServerTransferMethod);
            if (recallResult != "true")
            {
                TargetCreatureBase.SendMessage(recallResult, "server");
                TargetCreatureBase.PlaySound("Ding", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
            }
        }
    }
}
